from flask import Flask, render_template, request,flash,send_file,render_template_string

app = Flask(__name__)

@app.route('/')
def home():
    return render_template('form.html')  # 渲染 HTML 表单

@app.route('/submit', methods=['POST'])
def submit():
    # 获取表单数据
    user_input = request.form['user_input']
    # 处理数据（这里只是简单地返回它）
    return f'You submitted: {user_input}'

@app.route('/upload', methods=['POST'])
def upload_file():
    if 'file' not in request.files:
        flash('No file part')
        return "error"

    file = request.files['file']
    if file.filename == '':
        return 'No selected file'
    f=open("/home/chenfu/work/net/mid/mid.txt","w",encoding="utf-8")
    ss=file.readlines()
    f.write(str(len(ss))+"\n")
    f.close()
    return render_template('choice.html')
@app.route('/choose', methods=['POST'])
def choose():
    choice = request.form.get('choice', '')  # 获取用户的选择
    if choice:
        if choice.lower() == 'yes':
            return send_file("/home/chenfu/work/net/mid/mid.txt",as_attachment=True)
        elif choice.lower() == 'no':
            return 'You chose No!'
    return 'Please make a selection.'

if __name__ == '__main__':
    app.run("0.0.0.0",port=5000,debug=True)